package com.jfl.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jfl.annotation.Log;
import com.jfl.base.AbstractController;
import com.jfl.common.Constants;
import com.jfl.core.utils.ShiroUtils;
import com.jfl.enums.LogType;
import com.jfl.enums.Module;
import com.jfl.exception.BusinessException;
import com.jfl.model.SysDept;
import com.jfl.system.SysDeptService;

/**
 * 部门管理
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月11日]
 * @since V1.00
 */
@Controller
@RequestMapping("${jfl.module.system}/dept")
public class SysDeptController extends AbstractController
{
    @Reference(version = "${jfl.version}")
    private SysDeptService sysDeptService;
    
    @Value("${jfl.demoMode}")
    private boolean mode;
    /**
     * 部门跳转页面
     */
    @RequiresPermissions("system:dept:view")
    @GetMapping
    public String dept(ModelMap modelMap)
    {
        return Constants.MODULE_SYS_DEPT_PREFIX + "dept";
    }
    
    /**
     * 部门跳转页面
     */
    @RequiresPermissions("system:dept:list")
    @GetMapping("/list")
    @ResponseBody
    public List<SysDept> list(SysDept dept)
    {
        List<SysDept> deptList = this.sysDeptService.queryList(dept);
        return deptList;
    }
    
    /**
     * 修改
     * 
     * @param sysDept
     *            部门对象
     * @return
     */
    @GetMapping("/edit/{deptId}")
    public String edit(@PathVariable("deptId") Long deptId, ModelMap modelMap)
    {
        modelMap.put("dept", this.sysDeptService.queryById(deptId));
        return Constants.MODULE_SYS_DEPT_PREFIX + "edit";
    }
    
    /**
     * 修改
     * 
     * @param sysDept
     *            部门对象
     * @return
     */
    @Log(module = Module.DEPT, value = "修改部门", type = LogType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public ResponseEntity<ModelMap> editSave(SysDept sysDept, ModelMap modelMap)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysDeptService.update(sysDept);
        return setSuccessModelMap();
    }
    
    /**
     * 跳转页面
     * 
     * @param sysDept
     * @return
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        mmap.put("dept", this.sysDeptService.queryById(parentId));
        return Constants.MODULE_SYS_DEPT_PREFIX + "add";
    }
    
    /**
     * 新增保存部门
     * 
     * @param dept
     * @return
     */
    @Log(module = Module.DEPT, value = "添加部门", type = LogType.INSERT)
    @RequiresPermissions("system:dept:add")
    @PostMapping("/add")
    @ResponseBody
    public ResponseEntity<ModelMap> addSave(SysDept dept)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        dept.setCreateBy(ShiroUtils.getCurrentUserId());
        this.sysDeptService.update(dept);
        return setSuccessModelMap();
    }
    
    /**
     * 删除
     * 
     * @param deptId
     *            部门Id
     * @return
     */
    @Log(module = Module.DEPT, value = "删除部门", type = LogType.DELETE)
    @PostMapping("/remove/{deptId}")
    @ResponseBody
    public ResponseEntity<ModelMap> remove(@PathVariable("deptId") Long deptId)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysDeptService.deleteByDeptId(deptId);
        return setSuccessModelMap();
    }
    
    /**
     * 加载部门列表树
     * 
     * @return
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Map<String, Object>> treeData()
    {
        List<Map<String, Object>> tree = this.sysDeptService.selectDeptTree(new SysDept());
        return tree;
    }
    
    /** 部门树
     * @param deptId
     * @param modelMap
     * @return
     */
    @GetMapping("/selectDeptTree/{deptId}")
    public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap modelMap)
    {
        modelMap.put("dept", this.sysDeptService.queryById(deptId));
        return Constants.MODULE_SYS_DEPT_PREFIX + "tree";
    }
    
    /** 
     * 校验部门名称是否重复
     * @param dept
     * @return
     */
    @PostMapping("/checkDeptNameUnique")
    @ResponseBody
    public String checkDeptNameUnique(SysDept dept)
    {
        return this.sysDeptService.checkDeptNameUnique(dept);
    }
}
